# import requests
#
#
from utils.ip import get_proxy,get_proxy_poll,get_tunnel
import time

headers1 = {
    "Referer": "http://htgs.ccgp.gov.cn/GS8/contractpublish/index",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36",
    "X-Requested-With": "XMLHttpRequest"
}
# cookies = {
#     "$JSESSIONIDGS8": "wBR85jvnwi9sv9VVwnIqVczVP8CzqibB3MxCPBPoHf_OytK5JbyH\\u0021-1972094074",
#     "HMF_CI": "0d1d711fb366d48c6310c8d6588aa1114af701e85852f013b5888853e79fab312ba0bcd1b29987a6793acca53406532420c79b21ef9f37a6533234a525e0499681",
#     "cookieinsert": "20111186",
#     "HMY_JC": "f912a0ebacf7b4884c52296981065838976e043a188ba79817dbf90776fde62d32,",
#     "HBB_HC": "3537b17c0402123bae644272c58918e2c99f7d8474a1d03483696dc416ee8bd725b5a2564eec144974ff7e3a5edb3382e2"
# }
# url = "http://htgs.ccgp.gov.cn/GS8/contractpublish/getContractByAjax"
# params = {
#     "contractSign": "0"
# }

# response = requests.post(url, headers=headers, cookies=cookies, params=params, data=data, verify=False)
#
# print(response.json()['rows'])
#
# for i in response.json()['rows']:
#     print(i['contractName'])
# print(response)
import random

from curl_cffi import requests
cookies = {
    "$JSESSIONIDGS8": "wBR85jvnwi9sv9VVwnIqVczVP8CzqibB3MxCPBPoHf_OytK5JbyH\\u0021-1972094074",
    "HMF_CI": "0d1d711fb366d48c6310c8d6588aa1114af701e85852f013b5888853e79fab312ba0bcd1b29987a6793acca53406532420c79b21ef9f37a6533234a525e0499681",
    "cookieinsert": "20111186",
    "HBB_HC": "5091fff2a9a04fdee8c6943984a622619f6aad8eae64dbb9012499d2b4019a7d08",
    "CSH_DF": "7Ug6YfQRjL2MgImKryKIQpreSW2icTk6D8KtPvYTxkBdwtCdJkAF0o3dOfjUi6/xN6",
    "CSH_UF": "3c9fc7ddec9b58823c1c96756dbd45d8",
    "HMY_JC": "c690aafcabe37c3a401e70eb60330631a8c51a380907c0405836ce1d399bd40ced,"
}
headers = {

    "Referer": "http://htgs.ccgp.gov.cn/GS8/contractpublish/index",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36",
    "X-Requested-With": "XMLHttpRequest"
}
t = random.random()
while True:
    boolean = True

    try:
        url_get = f"http://htgs.ccgp.gov.cn/GS8/genCodeImg?t={t}"
        res = requests.get(url_get,headers= headers,proxies=get_tunnel())
        print(get_proxy())
        print(res)
        msf = res.json()['msg']
        for i in range(1,10):


            data = {
                "code": "pageNoChange",
                "currentPage": f"{i}",
                "codeResult": msf
            }
            try:
                url = "http://htgs.ccgp.gov.cn/GS8/contractpublish/getContractByAjax?contractSign=0"
                response = requests.post(url,headers=headers1,data=data,proxies=get_tunnel())
                if response.status_code == 200:
                    try:
                        print(response.json())
                        datas = response.json()['rows']
                        for data in datas:
                            print(data['contractName'])
                        boolean = False
                    except requests.exceptions.JSONDecodeError as e:
                        print(f"第{i}页json解析错误{e}")
                else:
                    print(f"第{i}页请求错误{response.status_code}")
            except Exception as e:
                print(f"第{i}页请求错误{e}")
                continue


    except requests.exceptions.ProxyError as e:
        print(f"代理异常 错误原因{e}")
    except Exception as e:
        print(f"错误原因{e}")
    if boolean == False:
        break
# for i in
#     print(i)